import axios from 'axios';

const ajax = axios.create({
  headers: { 'Content-Type': 'application/json' },
  responseType: 'json',
 ' Access-Control-Allow-Credentials': "true"
});


ajax.interceptors.response.use(
  (response) => {
    const { method } = response.config;

    const { err_no, err_msg, data } = response.data;

    switch (err_no) {
      case 0:
        return data;
      default:
        return Promise.reject(
          new Error(
            `[${err_no}] ${err_msg}\n${(method
              ? method
              : 'get'
            ).toUpperCase()} ${response.config.url}`
          )
        );
    }
  },
  error => {
    return Promise.reject(error);
  }
);

export default ajax;
